using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace HumanElement.Xna.Construct.GameState.MenuItems
{
    [DefaultProperty("Label")]
    [Description("A menu item that toggles between two states, like on/off, yes/no.")]
    public class ToggleMenuItem : MenuItem
    {
        private bool toggleValue;
        private string trueString;
        private string falseString;
        private string label;
        private string text;
        private const string ToggleOnStringDefault = "on";
        private const string ToggleOffStringDefault = "off";
        private const string LabelDefault = "Toggle";
        private const string Format = "{0}: {1}";

        public ToggleMenuItem()
        {
            this.trueString = ToggleOnStringDefault;
            this.falseString = ToggleOffStringDefault;
            // Use the property here so that text is set.
            this.Label = LabelDefault;
        }

        [Category("Appearance")]
        [DefaultValue(ToggleOnStringDefault)]
        public string ToggleOnString
        {
            get { return trueString; }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    throw new ArgumentException("Value must be a non-empty string.", "value");
                }
                if (this.trueString != value)
                {
                    this.trueString = value;
                    if (this.toggleValue)
                    {
                        this.text = String.Format(Format, this.label, this.trueString);
                    }
                }
            }
        }

        [Category("Appearance")]
        [DefaultValue(ToggleOffStringDefault)]
        public string ToggleOffString
        {
            get { return falseString; }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    throw new ArgumentException("Value must be a non-empty string.", "value");
                }
                if (this.falseString != value)
                {
                    this.falseString = value;
                    if (!this.toggleValue)
                    {
                        this.text = String.Format(Format, this.label, this.falseString);
                    }
                }
            }
        }

        [Category("Appearance")]
        [DefaultValue(false)]
        public bool ToggleValue
        {
            get { return this.toggleValue; }
            set
            {
                if (this.toggleValue != value)
                {
                    this.toggleValue = value;
                    if (this.toggleValue)
                    {
                        this.text = String.Format(Format, this.label, this.trueString);
                    }
                    else
                    {
                        this.text = String.Format(Format, this.label, this.falseString);
                    }
                }
            }
        }

        [Category("Appearance")]
        [DefaultValue(LabelDefault)]
        public string Label
        {
            get { return label; }
            set
            {
                if (this.label != value)
                {
                    this.label = value;
                    if (this.toggleValue)
                    {
                        this.text = String.Format(Format, this.label, this.trueString);
                    }
                    else
                    {
                        this.text = String.Format(Format, this.label, this.falseString);
                    }
                }
            }
        }

        public override string GetText()
        {
            return this.text;
        }

        protected internal override void OnSelected()
        {
            this.ToggleValue = !this.toggleValue;

            base.OnSelected();
        }
    }
}
